package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.EstCambioL;

public class EstCambioLDAOImpl extends JdbcDaoSupport implements IEstCambioLDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_EST_CAMBIO_L_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(EstCambioL estcambiol) {
		deleteOperation.execute( estcambiol  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public EstCambioL findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(EstCambioL estcambiol) {
		insertOperation.execute( estcambiol );
	}

	public void update(EstCambioL estcambiol) {
		updateOperation.execute( estcambiol );
	}

	public EstCambioL findByCriteria(EstCambioL  estcambiol) {
		return findByCriteriaQuery.runQuery(estcambiol);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public EstCambioL findByActiveState(EstCambioL  estcambiol) {
		return findByActiveStateQuery.runQuery(estcambiol);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	deleteOperation = new DeleteOperation(  getDataSource() );
    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_EST_CAMBIO_L_T ("
				  + " ID, "
				  + " ID_EST_SOCIO_ECONOMICO, "
				  + " CODIGO, "
				  + " ENTIDAD, "
				  + " ID_MOTIVO_CAMBIO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA, "
				  + ") VALUES ("
			  	  + " GES_EST_CAMBIO_L_T_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			compile();
		}
		public void execute(EstCambioL estcambiol) {
			InsertOperation.this.update( new Object[]{
				estcambiol.getId(),
				estcambiol.getIdEstSocioEconomico(),
				estcambiol.getCodigo(),
				estcambiol.getEntidad(),
				estcambiol.getIdMotivoCambio(),
				estcambiol.getUsuarioCrea(),
				estcambiol.getFechaCrea(),
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_EST_CAMBIO_L_T SET "
					  + " ID = ? ,"
					  + " ID_EST_SOCIO_ECONOMICO = ? ,"
					  + " CODIGO = ? ,"
					  + " ENTIDAD = ? ,"
					  + " ID_MOTIVO_CAMBIO = ? ,"
					  + " USUARIO_CREA = ? ,"
					  + " FECHA_CREA = ? ,"
				  	  + WHERE;
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			compile();
		}
		public void execute(EstCambioL estcambiol) {
			UpdateOperation.this.update( new Object[]{
				estcambiol.getId(),
				estcambiol.getIdEstSocioEconomico(),
				estcambiol.getCodigo(),
				estcambiol.getEntidad(),
				estcambiol.getIdMotivoCambio(),
				estcambiol.getUsuarioCrea(),
				estcambiol.getFechaCrea(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_EST_CAMBIO_L_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(EstCambioL estcambiol) {
			this.update( new Object[]{
				estcambiol.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_EST_CAMBIO_L_T "
										+ WHERE + " 1=1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			EstCambioL estcambiol = new EstCambioL();
				estcambiol.setId(new Long(rs.getLong("ID")));
				estcambiol.setIdEstSocioEconomico(new Long(rs.getLong("ID_EST_SOCIO_ECONOMICO")));
				estcambiol.setCodigo(rs.getString("CODIGO"));
				estcambiol.setEntidad(rs.getString("ENTIDAD"));
				estcambiol.setIdMotivoCambio(new Long(rs.getLong("ID_MOTIVO_CAMBIO")));
				estcambiol.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				estcambiol.setFechaCrea(rs.getDate("FECHA_CREA"));		
			return estcambiol;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public EstCambioL runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (EstCambioL) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_EST_SOCIO_ECONOMICO = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND ENTIDAD = ? "
			  	  	  					//+ " AND ID_MOTIVO_CAMBIO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public EstCambioL runQuery(EstCambioL estcambiol) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//estcambiol.getId(),
				//estcambiol.getIdEstSocioEconomico(),
				//estcambiol.getCodigo(),
				//estcambiol.getEntidad(),
				//estcambiol.getIdMotivoCambio(),
				//estcambiol.getUsuarioCrea(),
				//estcambiol.getFechaCrea(),
				});
			if ( result.size() > 0 ) {
        		return (EstCambioL) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE;
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public EstCambioL runQuery(EstCambioL estcambiol) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (EstCambioL) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
